from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework import generics,status
from questions_app.models import Question
from questions_app.serializer import AnswerQuestionSerializer,QuestionSerializer,AddQuestionViewSerializer,OneSectionQuestionSerializer
from user_app.utils import verify_token
from user_app.models import User
from rest_framework.response import Response
from course_app.models import Section

# Create your views here.
class QuestionView(generics.ListAPIView):
    queryset = Question.objects.filter(answers=None).order_by('-create_time')
    serializer_class = QuestionSerializer

#查询单个问题详情
class OneQuestionView(generics.RetrieveAPIView):
    queryset = Question.objects.filter(answers=None)
    serializer_class = QuestionSerializer

#查询单个章节的所有问题
class OneSectionQuestionView(generics.ListAPIView):
    def get_queryset(self):
        section_id = self.request.query_params.get('section')
        data = Question.objects.filter(section_id=section_id).filter(answers=None).all().order_by('-create_time')
        return data
    serializer_class = OneSectionQuestionSerializer

    # filterset_fields = ('section',)

#提问问题
class AddQuestionView(APIView):
    def post(self,request):
        #接收参数
        serializer = AddQuestionViewSerializer(data=request.data)
        #增加提问问题
        if serializer.is_valid():
            try:
                #校验token
                data = verify_token(request.META.get('HTTP_AUTHORIZATION'))
                #获取用户信息   
                user = User.objects.filter(phone=data.get('phone')).first()
                #获取课程小节信息
                section = Section.objects.filter(id=serializer.validated_data.get('section')).first()
            except Exception:
                return Response({'code':404,'msg':'用户信息有误',"status":status.HTTP_404_NOT_FOUND})
            #创建问题
            Question.objects.create(
                title=serializer.validated_data.get('title'),
                content=serializer.validated_data.get('content'),
                section = section,
                user = user
            )
            return Response({'code':200,'msg':'提交成功',"status":status.HTTP_200_OK})
        else:
            return Response({'code':400,'msg':'提交失败',"status":status.HTTP_400_BAD_REQUEST})
        
#回答问题
class AddAnswerView(APIView):
    def post(self,request):
        #接收参数
        serializer = AnswerQuestionSerializer(data=request.data)
        #增加提问问题
        if serializer.is_valid():
            try:
                #校验token
                data = verify_token(request.META.get('HTTP_AUTHORIZATION'))
                #获取用户信息   
                user = User.objects.filter(phone=data.get('phone')).first()
                #获取课程小节信息
                section = Section.objects.filter(id=serializer.validated_data.get('section')).first()
                #获取问题
                question = Question.objects.filter(id=serializer.validated_data.get('question')).first()
            except Exception:
                return Response({'code':404,'msg':'用户信息有误',"status":status.HTTP_404_NOT_FOUND})
            #创建问题
            Question.objects.create(
                content=serializer.validated_data.get('content'),
                answers = question,
                section = section,
                user = user
            )
            return Response({'code':200,'msg':'提交成功',"status":status.HTTP_200_OK})
        else:
            return Response({'code':400,'msg':'提交失败',"status":status.HTTP_400_BAD_REQUEST})